package com.bit.strongbaby;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.R.anim;
import android.R.integer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class KeyMeasureActivity extends Activity {

	private final float START_DRAG = 0;
	private final float END_DRAG = 1;
	private float isMoving;
	private float offset_y;
	private boolean start_yn = true;

	ImageView key_img;
	Button key_ok, key_bb1, key_bt1, key_bb2, key_bt2;
	EditText et;
	String data;
	float key_himg,key_img_f, key_img_b ;
	float key_re_f;
	static float key_re_b;
	public static float a;
	public static float popi = 0;
	public static float fti = 0;
	public static float fbi = 0;
	public static float bti = 0;
	public static float bbi = 0;
	public static String str1 = null;
	Bitmap bm=null;
	
	public static String ste; 


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_key_measure);


		key_img=(ImageView)this.findViewById(R.id.key_image);
		key_ok = (Button)this.findViewById(R.id.key_ok);


		Intent intent = getIntent();
		String st = intent.getStringExtra("aaa");			// aaa값을 keyactivity 에서 받아온다

		Uri ust = Uri.parse(st);				// 받아온 aaa값을 Uri형으로 형변환 후 ust로 넣는다

			try {
				bm = Images.Media.getBitmap(getContentResolver(), ust);
				key_img.setImageURI(ust);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

//========================================================================================= Uri값으로 이미지 불러오기△		

		setfather();		// 아빠 키 받아오기 

//========================================================================================= 확인 키 클릭 리스너	

		findViewById(R.id.key_ok).setOnClickListener(new OnClickListener() {
			int count = 0;
			public void onClick(View v) {

				float fbi_1 = 1080-fbi; 

				if(fbi_1 < 0) fbi_1 = fbi_1 * -1;

				count++;
				if(count == 1)
				{
					key_img_f = (fti + fbi_1)-1080;
					setbaby();				
				}

				float bbi_1 = 1080-fbi;
				
				if(bbi_1 < 0) bbi_1 = bbi_1 * -1;

				if(count == 2) 
				{
					key_img_b = (bti + bbi_1)-1030;
					popupwindew();	
				}

				if(count == 3){
					
					Intent ini = new Intent(getBaseContext(), GraphMainActivity.class);
					ste = String.format("%.1f", key_re_b);

					ini.putExtra("key",ste);

					startActivity(ini);	

				}
			}
		});
		
		//========================================================================================= 

	
	}
	//=====================================================================================  메소드 영역
	@SuppressLint("NewApi")
	public void setbaby()
	{
		key_bb1 = (Button)this.findViewById(R.id.key_bb1);
		key_bt1 = (Button)this.findViewById(R.id.key_bt1);
		key_bb2 = (Button)this.findViewById(R.id.key_bb2);
		key_bt2 = (Button)this.findViewById(R.id.key_bt2);

		key_bb1.setBackgroundColor(Color.TRANSPARENT);
		key_bt1.setBackgroundColor(Color.TRANSPARENT);

		key_bb2.setBackground(getResources().getDrawable(R.drawable.key_blue));
		key_bt2.setBackground(getResources().getDrawable(R.drawable.key_blue));		


		findViewById(R.id.key_bt2).setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {	
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (start_yn) {
						offset_y = event.getRawY();						
						start_yn = false;
					}

					isMoving = START_DRAG;

				} else if (event.getAction() == MotionEvent.ACTION_UP) {

					isMoving = END_DRAG;

				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if (isMoving == START_DRAG) {

						bti = (int)(((int) event.getRawY()+50) - offset_y);
						v.setY(bti+200);
					}
				}

				return false;
			}

		});

		findViewById(R.id.key_bb2).setOnTouchListener(new View.OnTouchListener() {


			@Override
			public boolean onTouch(View v, MotionEvent event) {	
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (start_yn) {

						offset_y = event.getRawY();						
						start_yn = false;
					}

					isMoving = START_DRAG;

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					isMoving = END_DRAG;

				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if (isMoving == START_DRAG) {
						bbi = (int) (((int) event.getRawY()+50) - offset_y);
						v.setY(bbi+200);
					}
				}
				return false;
			}
		});
	}

	//==========================================================================================================

	public void popupwindew()
	{
		int a=0;

		Context mContext = KeyMeasureActivity.this;

		AlertDialog.Builder bulder;
		AlertDialog dialog;
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.key_popup,(ViewGroup) findViewById(R.id.father_layout));

		et = (EditText)layout.findViewById(R.id.key_father_edit);

		bulder= new AlertDialog.Builder(mContext);
		bulder.setView(layout);

		dialog = bulder.create();
		dialog.setTitle("어른의 키를 입력해주세요");
		dialog.setButton("확인", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				data = et.getText().toString();

				key_re_f = Integer.parseInt(data);
				key_re_b = (key_img_b * key_re_f) / key_img_f;

				dialog.dismiss();
				
			}
		});
		dialog.show();
	}

	//==========================================================================================================

	public void setfather()
	{

		findViewById(R.id.key_bt1).setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {	
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (start_yn) {
						offset_y = event.getRawY();						
						start_yn = false;
					}
					isMoving = START_DRAG;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {

					isMoving = END_DRAG;

				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {

					if (isMoving == START_DRAG) {

						fti = (int) ((int) event.getRawY() - offset_y);

						v.setY(fti+200);
					}
				}
				return false;
			}

		});

		findViewById(R.id.key_bb1).setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {	
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (start_yn) {
						offset_y = event.getRawY();						
						start_yn = false;
					}
					isMoving = START_DRAG;
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					isMoving = END_DRAG;
				} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
					if (isMoving == START_DRAG) {
						fbi = (int) ((int) event.getRawY() - offset_y);

						v.setY(fbi+200);					
					}
				}
				return false;
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.key_measure, menu);
		return true;
	}


}

